/**
 * @(#) ConsumoComplessivo.java
 */

package it.unibo.iss.domotica.consumptionControlling;

public class TotalConsumption extends Subject
{
	private double currentConsumption;
	private double threshold;
	private boolean alert;
	
	public TotalConsumption(double threshold) {
		this.threshold = threshold;
		alert = false;
	}
	
	public boolean isAlert() {
		return alert;
	}
	
	public synchronized void setCurrentConsumption(double currentConsumption, boolean notify) {
		this.currentConsumption = currentConsumption;
		
		// Invia le notifiche solo se il consumo supera la soglia massima
		// e se non sono ancora state inviate le notifiche relative a quel
		// superamento. Quando il consumo complessivo rientra nei limiti,
		// al prossimo superamento verranno inviate nuove notifiche.
		if(this.currentConsumption <= threshold) {
			alert = false;
		}
		else if(this.currentConsumption > threshold && !alert) {
			alert = true;
			
			if(notify)
				sendNotify();
		}
	}
	
	public synchronized double getCurrentConsumption() {
		return currentConsumption;
	}
	
	public double getThreshold() {
		return threshold;
	}

}
